/*!
 * Copyright (c) 2010 WangYang
 * Registration Forms Authentication.
 * Date: 2011-01-14 20:42:16
 */
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
var passwdtest = /^[a-zA-Z0-9_]{6,20}$/;
var nametest = /^[a-zA-Z][a-zA-Z0-9|_]{4,20}$/;

jQuery(function(){
	jQuery("#loginid").keypress(function(){
		jQuery("#nametip").html("");
	});
	jQuery("#password").keypress(function(){
		jQuery("#passtip").html("");	
	});
})


function checkemail(email){
	var bool = true;
	jQuery.ajax({
		type: "POST",
		cache:false,
        async:false,
		url: "/templets/html/shop/jsp/checkPersonalEmail.jsp",
		data: "user_email="+email,
		success: function(data){
			 if(data!=null && data!=""){
				if( data == 1 ){
					jQuery("#p_email").css("color","red");
					jQuery("#p_email").html("该Email已被占用。");
					bool = false;
				} else{
					jQuery("#p_email").css("color","green");
					jQuery("#p_email").html("该Email可以使用。");
				}
			 }
		}

	});

	return bool;
}

function checkEmail(){
	var email = jQuery("#email").val();
	var checkEmail = "";
	if( email == '' ){
		jQuery("#p_email").css("color","red");
		jQuery("#p_email").html("请输入Email地址。");
		return false;
	}else if(!myreg.test(email)){
		jQuery("#p_email").css("color","red");
		jQuery("#p_email").html("请输入有效的Email地址。");
		return false;
	}else{
		jQuery("#p_email").css("color","green");
		jQuery("#p_email").html("该Email可以使用。");
		return checkemail(email);
	}
	return true;
}

function checkname(user_name){

	var bool = true;
	jQuery.ajax({
		type: "POST",
		cache:false,
        async:false,
		url: "/templets/html/shop/jsp/checkUserName.jsp",
		data:"user_name=" + user_name,
		success: function(data){
			 if(data!=null && data!=""){
				if( data == 1 ){
				jQuery("#p_user_name").css("color","red");
					jQuery("#p_user_name").html("用户名已经存在。");
					bool = false;
				} else{
				jQuery("#p_user_name").css("color","green");
					jQuery("#p_user_name").html("用户名可以使用。");
				}
			 }
		}
	});
	return bool;
}

function checkUser_name(){

	var user_name = jQuery("#loginid").val();
	if( user_name == '' ){
		jQuery("#p_user_name").css("color","red");
		jQuery("#p_user_name").html("请输入用户名。");
		return false;
	}else{
		jQuery("#p_user_name").css("color","green");
		jQuery("#p_user_name").html("用户名可以使用");
		return checkname(user_name);
	}
	return true;
}

var passwordUtil = {

	simplePassword:['123456','123456789','12345678','123123','5201314','1234567','7758521','654321','1314520','123321','1234567890','147258369','123654','5211314','woaini','1230123','987654321','147258','123123123','7758258','520520','789456','456789','159357','112233','1314521','456123','110110','521521','zxcvbnm','789456123','0123456789','0123456','123465','159753','qwertyuiop','987654','115415','1234560','123000','123789','100200','963852741','121212','111222','123654789','12301230','456456','741852963','asdasd','asdfghjkl','369258','863786','258369','8718693','666888','5845201314','741852','168168','iloveyou','852963','4655321','102030','147852369','321321'],

	isSimplePassword:function(password){
		var isSimplePassword = false;
		for(var i = 0,size = this.simplePassword.length; i < size; ++i ){
			if(this.simplePassword[i] == password){
				isSimplePassword = true;
				break;
			}
		}
		return isSimplePassword;
	}


};

var strUtil = {
	isAllSameChar:function(str){
		var c = str.charAt(0);
		var isAllSame = true;
		for(var i = 1,size = str.length; i < size; ++i){
			if(c != str.charAt(i)){
				isAllSame = false;
				break;
			}
		}
		return isAllSame;
	}
};

function checkPasswd1(){

	var passwd1 = jQuery("#password").val();
	if( passwd1 == '' ){
		jQuery("#passwordLabel1").css("color","red");
		jQuery("#passwordLabel1").html("请输入密码。");
		return false;
	}else if(passwd1.length < 6) {
		jQuery("#passwordLabel1").css("color","red");
		jQuery("#passwordLabel1").html("密码必须大于6位，请重新输入。");
		return false;
	}else if(!passwdtest.test(passwd1)) {
		jQuery("#passwordLabel1").css("color","red");
		jQuery("#passwordLabel1").html("密码只能为字母或数字，请重新输入。");
		return false;
	}else if(strUtil.isAllSameChar(passwd1) || passwordUtil.isSimplePassword(passwd1)){
		jQuery("#passwordLabel1").css("color","red");
		jQuery("#passwordLabel1").html("密码太简单，请尝试数字、字母和下划线的组合。");
		return false;
	}else{
		jQuery("#passwordLabel1").css("color","green");
		jQuery("#passwordLabel1").html("密码输入正确。");
	}
	return true;
}

function checkPasswd2(){

	var passwd1 = jQuery("#password").val();
	var passwd2 = jQuery("#confirmPassword").val();
	if( passwd2 == '' ){
		jQuery("#confirmPassword1").css("color","red");
		jQuery("#confirmPassword1").html("请输入确认密码。");
		return false;
	}else if(passwd2.length < 6) {
		jQuery("#confirmPassword1").css("color","red");
		jQuery("#confirmPassword1").html("密码必须大于6位，请重新输入。");
		return false;
	}else if(!passwdtest.test(passwd2)) {
		jQuery("#confirmPassword1").css("color","red");
		jQuery("#confirmPassword1").html("密码只能为字母或数字，请重新输入。");
		return false;
	}if( passwd1 != passwd2 ){
		jQuery("#confirmPassword1").css("color","red");
		jQuery("#confirmPassword1").html("两次密码不一致，请重新输入。");
		return false;
	}else{
		jQuery("#confirmPassword1").css("color","green");
		jQuery("#confirmPassword1").html("确认密码输入正确。");
	}
	return true;
}

function checkUserrand(){

	var userrand1 = jQuery("#userrand").val();
	if( userrand1 == ''){
		jQuery("#valcodeIcon").css("color","red");
		jQuery("#valcodeIcon").html("请输入验证码。");
		return false;
	}else if( jQuery('#userrand').val().length != 4){
		jQuery("#valcodeIcon").css("color","red");
		jQuery("#valcodeIcon").html("请输入正确的验证码。");
		return false;
	}else{
		return checkrand(userrand1);
	}
	return true;
}


function checkrand(userrand){

	var bool = true;
	jQuery.ajax({
		type: "POST",
		cache:false,
        async:false,
		url: "/templets/html/shop/jsp/checkUserrand.jsp",
		data:"userrand=" + userrand,
		success: function(data){
			 if(data!=null && data!=""){
				if( data == 0 ){
					jQuery("#valcodeIcon").css("color","red");
					jQuery("#valcodeIcon").html("请输入正确的验证码。");
					bool = false;
				} else{
					jQuery("#valcodeIcon").css("color","green");
					jQuery("#valcodeIcon").html("验证码输入正确。");
				}
			 }
		}
	});
	return bool;
}

function changeCode(){
	jQuery("#rc").attr("src","/checkImage" + "?" + Math.random());
}

function Check_User_Login(){
		 if(!checkEmail()) return false;
		 if(!checkUser_name()) return false;
		 if(!checkPasswd1()) return false;
	     if(!checkPasswd2()) return false;
	     if(!checkUserrand()) return false;
         document.loginForm.submit();
}



